/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.session;

import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import jpa.entities.Course;
import jpa.entities.Topic;

/**
 *
 * @author KenShin
 */
@Stateless
public class CourseFacade extends AbstractFacade<Course> {
    @PersistenceContext(unitName = "SupportiveLearning-ejbPU")
    private EntityManager em;
    private Course course;
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CourseFacade() {
        super(Course.class);
    }
    public Topic getTopicFromId(int topicId)
    {
        try
        {
        Query query = em.createQuery("SELECT t FROM Topic t WHERE t.topicId = :topicId");
        query.setParameter("topicId", topicId);
        return (Topic)query.getSingleResult();
        }catch(Exception ex)
        {
        ex.printStackTrace();
        }
        return null;
    }
    public List<Course> findCourseByTopicId(Topic topic) {
        
                Query query = em.createNamedQuery("Course.findCourseByTopicId");
                query.setParameter("tId", topic);
    return (List<Course>) query.getResultList();
    }
    public List<Course> showAllCourse() {
                Query query = em.createQuery("SELECT c FROM Course c order by c.startDate desc");
    return (List<Course>) query.getResultList();
    }
    public List<Course> showTop3Course() {
                Query query = em.createQuery("SELECT c FROM Course c order by c.startDate desc");
    return (List<Course>) query.getResultList();
    }
    public Course getCourseFromCourseName(String courseName)
    {
         List<Course> results = em.createQuery("SELECT c FROM Course c WHERE c.courseName = :courseName")
           .setParameter("courseName", courseName)
           .getResultList();
         
         if (!results.isEmpty() ) {
           course = results.get(0);
        }
        return course;
    }

}